package com.kz.himalaya.activity;

import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.kz.himalaya.R;
import com.kz.himalaya.adapter.TabAdapter;
import com.kz.himalaya.application.MyApplication;
import com.kz.himalaya.bean.TabsData;
import com.kz.himalaya.bean.details.DetailListEntity;
import com.kz.himalaya.databinding.ActivityItemDetailsBinding;
import com.kz.himalaya.databinding.DetailTitleBarBinding;
import com.kz.himalaya.fragment.homepage.DetailFragment;
import com.kz.himalaya.fragment.homepage.ProgramListFragment;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ItemDetailsActivity extends AppCompatActivity implements Callback<DetailListEntity>, View.OnClickListener {

    private long albumId;
    private ActivityItemDetailsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_item_details);
        albumId = getIntent().getLongExtra("albumId",-1);
        if (albumId!=-1) {
            ((MyApplication) getApplication()).getService().getDetailList(albumId).enqueue(this);
        }

    }

    @Override
    public void onResponse(Call<DetailListEntity> call, Response<DetailListEntity> response) {
        DetailListEntity body = response.body();
        if (body != null) {
                binding.setDetail(body.getData());
            TabsData.Tab tab1 = new TabsData().new Tab();
            TabsData.Tab tab2 = new TabsData().new Tab();
            tab1.setTitle("详情");
            tab2.setTitle("节目("+body.getData().getAlbum().getTracks()+")");
            List<TabsData.Tab> list = new ArrayList<>();
            list.add(tab1);
            list.add(tab2);
            List<Fragment> fragments = new ArrayList<>();
            fragments.add(DetailFragment.newInstance(albumId));
            fragments.add(ProgramListFragment.newInstance(albumId));
            TabAdapter adapter = new TabAdapter(getSupportFragmentManager(), list, fragments);
            binding.pager.setAdapter(adapter);
            binding.tabDetail.setupWithViewPager(binding.pager);
            DetailTitleBarBinding barBinding = binding.titleBar;
            barBinding.back.setOnClickListener(this);
        }
    }

    @Override
    public void onFailure(Call<DetailListEntity> call, Throwable t) {
        t.printStackTrace();
        Log.i("1620", "onFailure: "+t.getMessage());
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
